import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class Studentitempre63 {
    private static ArrayList<Userss> list = new ArrayList<>();
    public static void main(String[] args) {
            System.out.println("-------------欢迎来到学生管理系统------------------");

            Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请选择操作 1登录 2注册 3忘记密码");

            String choose = sc.nextLine();
            switch (choose) {
                case "1" -> land(list);
                case "2" -> register(list);
                case "3" -> forgetpassword(list);
                case "4" -> {
                    System.out.println("谢谢,没有这个选项");
                    System.exit(0);
                }
                default -> System.out.println("没有这个选项");
            }
        }
    }
    public static void land(ArrayList<Userss> list) {
        System.out.println("登录");
        for (int i = 0; i < 3; i++) {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入用户名");
            String username = sc.next();
            boolean flag = contains(list, username);
            if (!flag) {
                System.out.println("用户名" + username + "未注册,请先注册再登陆");
                return;
            }
            System.out.println("请输入密码");
            String password = sc.next();

            while (true) {
                String ringhtcode = getCode();
                System.out.println("请输入验证码" + ringhtcode);
                System.out.println("请输入验证码");
                String code = sc.next();
                if (code.equals(ringhtcode)) {
                    System.out.println("您输入的验证码正确");
                    break;
                } else {
                    System.out.println("验证码错误");
                    continue;
                }
            }
            Userss Userinfo = new Userss(username, password, null, null);
            boolean result = checkUserInfo(list, Userinfo);
            if (result) {
                System.out.println("登录成功");
                Studentitem62 ss = new Studentitem62();
                ss.startStudentSystem();
                break;
            } else {
                System.out.println("登录失败,用户名或密码错误");
                if (i == 2) {
                    System.out.println("当前账号" + username + "被锁定,请联系客服");
                    return;
                } else {
                    System.out.println("用户名或密码输入错误,还剩下" + (2 - i) + "次机会");
                }
            }
        }
    }
    private static boolean checkUserInfo(ArrayList<Userss> list, Userss userinfo) {
        for (int i = 0; i < list.size(); i++) {
            Userss user = list.get(i);
            if(user.getUsername().equals(userinfo.getUsername())&&user.getPassword().equals(userinfo.getPassword())){
                return true;
            }
        }
        return false;
    }

    public static void register(ArrayList< Userss > list){
        System.out.println("注册");
        Userss u = new Userss();
        Scanner sc = new Scanner(System.in);
        while(true) {
            System.out.println("请输入用户名");
            String username = sc.next();
            u.setUsername(username);
            //开发细节:先验证格式是否正确,再验证是否唯一
            boolean flag = checkusername(username);
            if (!flag) {
                System.out.println("您输入的用户名不满足条件,请重新输入");
                continue;
            }
            boolean flag2 = contains(list, username);
            if (flag2) {
                System.out.println("用户名已存在,请重新输入");
            } else {
                System.out.println("当前用户名" + username + "可用");
                break;
            }
            u.setUsername(username);
        }
        while(true){
            System.out.println("请输入密码");
            String password= sc.next();
            u.setPassword(password);
            System.out.println("请第二次输入密码");
            String password2 = sc.next();
            if(!password.equals(password2)){
                System.out.println("两次密码输入不一致,请重新输入");
            }else{
                System.out.println("两次密码输入一致,密码输入成功");
               break;
            }
        }
        while(true) {
            System.out.println("请输入身份证号码");
            String personid= sc.next();
            u.setPersonid(personid);
            boolean flag3 = checkPersonId(list,personid);
            if (!flag3) {
                System.out.println("您输入的身份证号码不符合要求");
            }else{
                System.out.println("输入身份证号成功");
                break;
            }
        }
        while (true) {
            System.out.println("请输入手机号码");
            String phonenumber= sc.next();
            u.setPhonenumber(phonenumber);
            boolean flag4 = checkPhoneNumber(list,phonenumber);
            if (!flag4) {
                System.out.println("您输入的手机号码有误,请重新输入");
            }
            else{
                System.out.println("输入手机号码成功");
                break;
            }
        }
        list.add(u);
        System.out.println("注册成功");
        printlist(list);
    }
    public static void forgetpassword(ArrayList<Userss> list){
        System.out.println("忘记密码");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = sc.next();
        boolean flag = contains(list, username);
        if (!flag) {
            System.out.println("您输入的用户名还未注册");
            return;
        }
        System.out.println("请输入身份证号");
        String personid = sc.next();
        System.out.println("请输入手机号码");
        String phonenumber = sc.next();
        int index = findIndex(list,username);
        Userss user = list.get(index);
        if(!(user.getPersonid().equals(personid)&&user.getPhonenumber().equals(phonenumber))){
            System.out.println("身份证号码或手机号码输入有误,无法修改密码");
        return;
        }
        String password;
        while (true) {
            System.out.println("请输入新的密码");
            password = sc.next();
            System.out.println("请再次输入新的密码");
            String password2 = sc.next();
            if(!password.equals(password2)){
                System.out.println("两次密码输入不一致,请重新输入");
                continue;
            }else{
                System.out.println("两次密码输入一致");
                break;
            }
        }
      user.setPassword(password);
        System.out.println("密码修改成功");
    }

    private static int findIndex(ArrayList<Userss> list, String username) {
        for (int i = 0; i < list.size(); i++) {
            Userss user = list.get(i);
            if (user.getUsername().equals(username)) {
                return i;
            }
        }
        return -1;
    }


    private static void printlist(ArrayList<Userss> list){
        for(int i=0; i<list.size(); i++){
            Userss u = list.get(i);
            System.out.println(u.getUsername()+" "+u.getPassword()+" "+u.getPersonid()+" "+u.getPhonenumber());
        }
    }
    public static boolean checkusername(String username) {
        int count = 0;
        //用户码长度为3~15
        if(username.length() < 3 || username.length() > 15) {
            return false;
        }
        for (int i = 0; i < username.length(); i++) {
            char ch = username.charAt(i);
            if (!((ch >= 'a' && ch <= 'z') || (ch >= 'A') || (ch <= 'Z') || (ch >= '0' && ch <= '9')))
                return false;
        }
        for (int i = 0; i < username.length(); i++) {
            char ch = username.charAt(i);
            if (ch >= 'a' && ch <= 'z'|| ch >= 'A' && ch <= 'Z') {
                count++;
                break;
                }
            }
         return count > 0;
        }
    public static boolean contains(ArrayList<Userss> list,String username){
        for (int i = 0; i < list.size(); i++){
          Userss str = list.get(i);
          String str1 = str.getUsername();
          if(str1.equals(username)){
              return true;
          }
        }
        return false;
    }
    public static boolean checkPersonId(ArrayList<Userss> list,String personid){
        if(personid.length() != 18){
            return false;
        }
        for (int i = 0; i < 1; i++){
            char c = personid.charAt(i);
            if(c == '0'){
                return false;
            }
        }
        for (int i = 0; i < list.size()-1; i++){
            char c = personid.charAt(i);
            if(!(c >= '0'&&c <= '9')){
                return false;
            }
        }
        for (int i = 17; i < list.size(); i++){
            char c = personid.charAt(i);
            if(!((c >= '0'&&c <= '9')||(c == 'x')||(c == 'X'))){
                return false;
            }
        }
        return true;
    }
    public static boolean checkPhoneNumber(ArrayList<Userss> list,String phonenumber) {
        if (phonenumber.length() != 11) {
            return false;
        }
        if(phonenumber.startsWith("0")){
            return false;
        }
        for (int i = 0; i < phonenumber.length(); i++) {
            char ch = phonenumber.charAt(i);
            if (!(ch >= '0' && ch <= '9')) {
                return false;
            }
        }
        return true;
    }
    //生成一个验证码
    private static String getCode(){
        ArrayList<Character> list = new ArrayList<>();
        for(int i = 0; i<26; i++){
            list.add((char)('a'+i));
            list.add((char)('A'+i));
        }
        StringBuilder sb = new StringBuilder();
        Random r = new Random();
        for(int i = 0; i < 4;i++) {
            int index = r.nextInt(list.size());
            char ch = list.get(index);
            sb.append(ch);
        }
            int number = r.nextInt(10);
            sb.append(number);
            char[] arr = sb.toString().toCharArray();
            int index2 = r.nextInt(arr.length);
            char temp = arr[index2];
            arr[index2] = arr[arr.length-1];
            arr[arr.length-1] = temp;
            return new String(arr);
    }
}

